﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Somnus.Core;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;

namespace Somnus.Objects.GameObjects.LevelObjects
{
    class Wind : Sensor
    {
        /// <summary>
        /// Tempo de duração do vento
        /// valor negativo faz com que o vento dure para sempre
        /// </summary>
        public int lifeTime;

        /// <summary>
        /// Velocidade do vento em metros por segundo
        /// </summary>
        public float windVelocity;

        private int lifeTimer;

        public Wind(DreamWorld world, String name, Texture2D texture, Nullable<Rectangle> sourceRectangle, BodyType bodyType, float x, float y, float width, float height, int lifeTime)
            : base(world, name, texture, sourceRectangle, bodyType, x, y, width, height)
        {
            this.lifeTime = lifeTime;
            lifeTimer = 0;
            windVelocity = 0.3f;
        }

        public override void update(GameTime gameTime)
        {
            if (lifeTimer >= lifeTime)
            {
                this.kill = true;
            }
            else
            {
                lifeTimer++;
            }

            base.update(gameTime);
        }

        protected override bool HandleBeginContact(Fixture thisFixture, Fixture otherFixture, Contact contact)
        {
            return base.HandleBeginContact(thisFixture, otherFixture, contact);
        }

        protected override void HandleIsColliding(Fixture thisFixture, Fixture otherFixture)
        {
            PhysicsObject po = otherFixture.UserData as PhysicsObject;
            Vector2 velocity = po.body.LinearVelocity;
            velocity.X += (float)(Math.Sin(MathHelper.ToRadians(this.Rotation)) * windVelocity);
            velocity.Y += -(float)(Math.Cos(MathHelper.ToRadians(this.Rotation)) * windVelocity);
            velocity.Y += -0.2f;
            po.body.LinearVelocity = velocity;
            base.HandleIsColliding(thisFixture, otherFixture);
        }
    }
}
